//
//  ICMCellPartView.m
//  ICrEAM
//
//  Created by rainimute on 16/11/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMCellPartView.h"
#import "ICMCellPartCell.h"

@interface ICMCellPartView ()<UICollectionViewDelegate,UICollectionViewDataSource,ICMCellPartCellDelegate>

@property (nonatomic, strong) ICMProxyActivityViewModel *viewModel;//活动model

@property (nonatomic, strong) UICollectionView *cellCollectionView;//collectionview

@property (nonatomic, strong) UILabel *cellPartLabel;//标题label

//根据活动里面项目和零件  和   项目和零件  生成对应关系数据字典
@property (nonatomic, strong) NSMutableDictionary *labourAndPartsExistDic;

@end

@implementation ICMCellPartView

+ (instancetype)createCellPartViewWithViewModel:(ICMProxyActivityViewModel *)viewModel{
    return [[ICMCellPartView alloc]initCellPartViewWithViewModel:viewModel];
}

- (instancetype)initCellPartViewWithViewModel:(ICMProxyActivityViewModel *)viewModel{
    if (self = [super init]) {
        
        [self setupCellPartViewWithViewModel:viewModel WithExistDic:nil];
    }
    return self;
}

#pragma mark 根据model设值
- (void)setupCellPartViewWithViewModel:(ICMProxyActivityViewModel *)viewModel  WithExistDic:(NSMutableDictionary *)dic{
    self.labourAndPartsExistDic = dic;
    self.viewModel = viewModel;
    self.cellCollectionView.hidden = !viewModel.isRotation;
    self.cellPartLabel.hidden = !viewModel.isRotation;
    
    NSInteger partsCount = self.viewModel.allActivityModel.TT_ACTIVITY_PART.count;
    NSInteger lineCount = (partsCount%3==0)?(partsCount/3):(partsCount/3+1);
    self.cellCollectionView.frame = CGRectMake(200, 0, ScreenWidth-300, ActualHeight(100)*lineCount);
    self.hidden = (partsCount == 0);
    [self.cellCollectionView reloadData];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.viewModel.allActivityModel.TT_ACTIVITY_PART.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ICMCellPartCell *cell = [self.cellCollectionView dequeueReusableCellWithReuseIdentifier:@"ICMCellPartCell" forIndexPath:indexPath];
    
    ICMProxyActivityPartsModel *partsModel = self.viewModel.allActivityModel.TT_ACTIVITY_PART[indexPath.row];
    [cell setupPartsCellWithPartsModel:partsModel WithExistDic:self.labourAndPartsExistDic];
    cell.delegate = self;
    return cell;
}


- (UICollectionView *)cellCollectionView{
    if (!_cellCollectionView) {
        NSInteger partsCount = self.viewModel.allActivityModel.TT_ACTIVITY_PART.count;
        NSInteger lineCount = (partsCount%3==0)?(partsCount/3):(partsCount/3+1);
        
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((ScreenWidth-300)/3, ActualHeight(100));
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        
        _cellCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(200, 0, ScreenWidth-300, ActualHeight(100)*lineCount) collectionViewLayout:layout];
        [_cellCollectionView registerNib:[UINib nibWithNibName:@"ICMCellPartCell" bundle:nil] forCellWithReuseIdentifier:@"ICMCellPartCell"];
        _cellCollectionView.delegate = self;
        _cellCollectionView.dataSource = self;
        _cellCollectionView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_cellCollectionView];
        
    }
    return _cellCollectionView;
}

- (UILabel *)cellPartLabel{
    if (!_cellPartLabel) {
        _cellPartLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, 200, 20)];
        [_cellPartLabel setUpLabelWithText:@"维修零件" alignment:NSTextAlignmentCenter textSize:17 textColor:TitleColor_Dark];
        [self addSubview:_cellPartLabel];
    }
    return _cellPartLabel;
}

//ICMCellPartCellDelegate
#pragma mark 点击选择按钮回调方法
- (void)didSelectedStateButtonWithPartCode:(NSString*)PartCode andButtonState:(BOOL)isSelected WithActCode:(NSString *)actCode
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ProxyActivityPartChanged" object:self userInfo:@{@"Activity_Code":actCode,@"PartCode":PartCode,@"IsSelected":@(isSelected)}];
}


@end













